import axios from 'axios'
axios.defaults.baseURL = 'https://note-server.hunger-valley.com'
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
axios.defaults.withCredentials = true
import configBaseURL from '@/helpers/config-baseURL'

import { Message } from 'element-ui'
export default function request(url, type = 'GET', data = {}) {

    return new Promise((resolve, reject) => {
        let option = {
            url,
            method: type,
            ValidityState(status) {
                return (status>=200 && status<300) || status===400
            }
            
        }
        if (type.toLowerCase == 'get') {
            option.params=data
        } else {
            option.data=data
        }

        axios(option).then(res => {
            if (res.status === 200) {
                resolve(res.data)
                
            } else {
                Message.error(res.data.msg)
                reject(res.data)
            }
            
        }).catch(res => {
           
            Message.error(res.msg)
            reject({msg: '网络异常'})
        })

    })
    

}

